package com.health.dao;

import com.health.pojo.Users;
import com.health.utils.ConnectionUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class EditInformationDao {
    // 查询用户信息回填方法
    public Users searchInformation(Users user, int id) {
        Connection connection = ConnectionUtil.getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "select * from users where user_id = ?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);

            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                user.setUserAccount(resultSet.getString("user_account"));
                user.setUserName(resultSet.getString("user_name"));
                user.setUserPassword(resultSet.getString("user_password"));
                user.setUserGender(resultSet.getString("user_gender"));
                user.setUserImg(resultSet.getString("user_img"));
                return user;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("SQL错误！");
            user.setUserId(-1);
        }
        return user;
    }

    //  更新用户个人信息数据
    public boolean updateData(Users users) {
        Connection connection = ConnectionUtil.getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "update users set user_account=?,user_name=?,user_password=?,user_gender=? where user_id=?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, users.getUserAccount());
            preparedStatement.setString(2, users.getUserName());
            preparedStatement.setString(3, users.getUserPassword());
            preparedStatement.setString(4, users.getUserGender());
            preparedStatement.setInt(5, users.getUserId());
            int resultSet = preparedStatement.executeUpdate();
            if (resultSet == 1)
                return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("SQL错误！");
            return false;
        }
        return false;

    }
}
